package com.hhq.common.domain.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * <p>Description: 通用结果vo </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/1/5 14:50
 */
@Setter
@Getter
public class CommonResultVO<T> implements Serializable {
    /**
     * 成功
     */
    private static final String CODE_SUCCESS = "0";
    /**
     * 失败
     */
    private static final String CODE_ERROR = "-1";
    /**
     * 请求成功
     */
    public static final String SUCCESS_MSG = "请求成功";

    /**
     * 状态码  0:成功 -1:失败
     */
    @ApiModelProperty(value = "状态码", required = true, example = "0:成功 -1:失败")
    private String code;

    /**
     * 描述信息
     */
    @ApiModelProperty(value = "描述信息", required = true, example = "请求成功")
    private String msg;

    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据", required = true, example = "")
    private T data;

    /**
     * 接口响应时间(毫秒)
     */
    @ApiModelProperty(value = "接口响应时间(毫秒)", required = true, example = "1")
    private Long responseTime;


    /**
     * 成功
     *
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> ok() {
        CommonResultVO<T> result = new CommonResultVO<>();
        result.setCode(CODE_SUCCESS);
        result.setData(null);
        result.setMsg(SUCCESS_MSG);
        return result;
    }

    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> ok(T data) {
        CommonResultVO<T> result = new CommonResultVO<>();
        result.setCode(CODE_SUCCESS);
        result.setData(data);
        result.setMsg(SUCCESS_MSG);
        return result;
    }

    /**
     * 失败
     *
     * @param msg 失败消息
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> err(String msg) {
        CommonResultVO<T> result = new CommonResultVO<>();
        result.setCode(CODE_ERROR);
        result.setMsg(msg);
        return result;
    }

    /**
     * 失败
     *
     * @param code code码
     * @param msg  失败消息
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> err(String code, String msg, T data) {
        CommonResultVO<T> result = new CommonResultVO<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 失败
     *
     * @param code code码
     * @param msg  失败消息
     * @param <T>
     * @return
     */
    public static <T> CommonResultVO<T> err(String code, String msg) {
        CommonResultVO<T> result = new CommonResultVO<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
